from datetime import datetime

from django.db import models
from django.db.models import Model, Q


# Create your models here.

# 定义部门表
class Department(models.Model):
    # 定义一个CharField字段，用于存储部门的名称，verbose_name为中文提示，max_length为最大长度
    title = models.CharField(verbose_name="部门名称", max_length=32)


class InfoManager(models.Manager):
    def search(self, query):
        # 映射显示值到整数值
        level_mapping = {"普通": 1, "重要": 2, "紧急": 3}
        status_mapping = {"未开始": 1, "进行中": 2, "已完成": 3}

        # 初始化查询条件
        query_obj = Q()

        # 检查查询字符串是否匹配level或status的显示值，并添加到查询条件
        if query in level_mapping:
            query_obj |= Q(level=level_mapping[query])
        if query in status_mapping:
            query_obj |= Q(status=status_mapping[query])

        # 如果查询字符串不匹配预设的显示值，则使用其他字段进行搜索
        if query not in level_mapping and query not in status_mapping:
            query_obj |= Q(name__icontains=query) | Q(create_time__icontains=query) | Q(id__icontains=query)
            query_obj |= Q(price__icontains=query) | Q(customer__icontains=query) | Q(phone__icontains=query)

        return self.filter(query_obj)

    def select(self, query):
        gender_mapping = {"男": 1, "女": 2}
        query_obj = Q()
        if query in gender_mapping:
            query_obj |= Q(gender=gender_mapping[query])
        if query not in gender_mapping:
            query_obj |= Q(id__icontains=query) | Q(name__icontains=query) | Q(age__icontains=query) | Q(
                depart_id__title__icontains=query)
        return self.filter(query_obj)


# 定义员工表
class UserInfo(models.Model):
    # 定义一个CharField字段，存储用户的姓名，verbose_name为中文提示，max_length为最大长度
    name = models.CharField(verbose_name="姓名", max_length=32)
    # 定义一个CharField字段，存储用户的密码，verbose_name为中文提示，max_length为最大长度
    password = models.CharField(verbose_name="密码", max_length=64)
    # 定义一个IntegerField字段，存储用户的年龄，verbose_name为中文提示
    age = models.IntegerField(verbose_name="年龄")
    # 定义一个DecimalField字段，存储用户的薪资，verbose_name为中文提示，max_digits为最大数字位数，decimal_places为小数位数
    salary = models.DecimalField(verbose_name="薪资", max_digits=10, decimal_places=2)
    # 定义一个DateTimeField字段，存储用户的入职时间，verbose_name为中文提示
    create_time = models.DateField(verbose_name="入职时间")
    # 定义一个SmallIntegerField字段，存储用户的性别，verbose_name为中文提示，choices为性别选项
    gender = models.SmallIntegerField(verbose_name="性别", choices=((1, "男"), (2, "女")))
    # 定义一个ForeignKey字段，存储用户的部门，verbose_name为中文提示，to为关联的Department模型，to_field为关联的id字段，null为是否可以为空，blank为是否可以为空
    depart = models.ForeignKey(verbose_name="部门", to="Department", to_field="id", null=True, blank=True,
                               on_delete=models.SET_NULL)
    objects = InfoManager()


# 定义业务表
class BusinessInfo(models.Model):
    name = models.CharField(verbose_name="业务名称", max_length=128)
    create_time = models.DateField(verbose_name="创建时间")
    price = models.IntegerField(verbose_name="价格")
    customer = models.CharField(verbose_name="甲方公司负责人", max_length=32)
    phone = models.CharField(verbose_name="甲方公司联系方式", max_length=32)
    level = models.SmallIntegerField(verbose_name="业务级别", choices=((1, "普通"), (2, "重要"), (3, "紧急")),
                                     default=1)
    status = models.SmallIntegerField(verbose_name="业务状态", choices=((1, "未开始"), (2, "进行中"), (3, "已完成")),
                                      default=1)
    objects = InfoManager()


# 管理员表
class AdminInfo(models.Model):
    username = models.CharField(verbose_name="用户名", max_length=32, unique=True)
    password = models.CharField(verbose_name="密码", max_length=64)
    create_time = models.DateField(verbose_name="创建时间", default=datetime.now)
    login_time = models.DateTimeField(verbose_name="登录时间", default=datetime.now, null=True)
    login_IP = models.CharField(verbose_name="登录IP", max_length=32, null=True)
